﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Util.Log
{
    public class Logger
    {
        public static Logger Instance { private set; get; }

        private static readonly string LoggerFilePath = "proxy-log.txt";
        private FileInfo Logfile { get; set; }

        public event Action<LogLevel, string, string> LogEvents;

        static Logger()
        {
            Logger logger = new Logger();
            if (logger != null && logger.Init())
            {
                Instance = logger;
            }
        }

        private bool Init()
        {
            this.Logfile = new FileInfo(LoggerFilePath);
            this.LogEvents += ConsoleLog;
            this.LogEvents += FileLog;
            return true;
        }

        public void Logd(string message, string reason = null)
        {
            this.LogEvents(LogLevel.Debug, message, reason);
        }

        public void Logi(string message, string reason = null)
        {
            this.LogEvents(LogLevel.Info, message, reason);
        }
        public void Logi(string format, params object[] args)
        {
            string message = string.Format(format, args);
            this.LogEvents(LogLevel.Info, message, null);
        }

        public void Logw(string message, string reason = null)
        {
            this.LogEvents(LogLevel.Warn, message, reason);
        }

        public void Loge(string message, string reason = null)
        {
            this.LogEvents(LogLevel.Error, message, reason);
        }

        public void Log(LogLevel level, string message, string reason = null)
        {
            this.LogEvents(level, message, reason);
        }

        public void Loge(Exception e)
        {
            LogLevel level = LogLevel.Error;
            string message = e.Message;
            string reason = e.StackTrace;
            this.Log(level, message, reason);
        }

        private void ConsoleLog(LogLevel level, string message, string reason)
        {
            string log = level + ":" + message;
            if (reason != null)
            {
                log += " reason:" + reason;
            }
            Console.WriteLine(log);
        }

        private void FileLog(LogLevel level, string message, string reason)
        {

        }
    }
}
